package net.eyra.struts.actions;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.eyra.negocio.dao.EyraDaoException;
import net.eyra.negocio.entidades.gestion.propietarios.Propietario;
import net.eyra.negocio.entidades.gestion.propietarios.afeccion.Afeccion;
import net.eyra.negocio.entidades.gestion.propietarios.afeccion.PeriodoAcuerdo;
import net.eyra.negocio.entidades.gestion.propietarios.fincas.Finca;
import net.eyra.negocio.propietarios.Fincas;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.google.gson.Gson;

public class FincaAction extends DispatchAction {

    private Fincas fincas = new Fincas();

    public ActionForward CargarFincas(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
                                                                                                                                       throws IOException {

        try {
            List<Finca> _fincas = fincas.ListaNombreFincas();

            Gson json = new Gson();

            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/plain");
            response.getWriter().write(json.toJson(_fincas));
        } catch (EyraDaoException e) {
        }
        return null;
    }

    public ActionForward CargarDatosFinca(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
                                                                                                                                           throws IOException {

        int id_finca = Integer.parseInt(request.getParameter("id_finca"));

        try {
            Finca finca = fincas.DatosFinca(id_finca);

            for (Propietario propietario : finca.getPropietarios()) {
                propietario.setPropiedades(null);
                propietario.setCuentas_bancarias(null);
                propietario.setFacturas(null);
            }
            for (Afeccion afeccion : finca.getAfecciones()) {
                afeccion.getAcuerdo().setParque(null);
                afeccion.getAcuerdo().setAfecciones(null);
                afeccion.setFinca(null);
                for (PeriodoAcuerdo periodo : afeccion.getAcuerdo().getPeriodos())
                    periodo.setAcuerdo(null);
            }
            finca.setParque(null);

            Gson json = new Gson();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/plain");
            response.getWriter().write(json.toJson(finca));
        } catch (EyraDaoException e) {
        }

        return null;
    }

    public ActionForward GuardarDatosFinca(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
                                                                                                                                            throws IOException {

        try {
            int id_finca = Integer.parseInt(request.getParameter("id_finca"));

            Finca finca = fincas.DatosFinca(id_finca);

            fincas.GrabarFinca(finca);

            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/plain");
            response.getWriter().write("success");
        } catch (Exception e) {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/plain");
            response.getWriter().write(e.toString());
        }

        return null;
    }
}